Kata
Given: an array containing hashes of names
Return: a string formatted as a list of names separated by commas except for the last two names, which should be separated by an ampersand.
Example:1
2
3
4
5
6
7
8
9
10
11list([ {name: 'Bart'}, {name: 'Lisa'}, {name: 'Maggie'} ])
// returns 'Bart, Lisa & Maggie'
list([ {name: 'Bart'}, {name: 'Lisa'} ])
// returns 'Bart & Lisa'
list([ {name: 'Bart'} ])
// returns 'Bart'
list([])
// returns ''
Note: all the hashes are pre-validated and will only contain A-Z, a-z, ‘-‘ and ‘.’.
# My Solutions
1 | function list(names){ |
# Others
마지막 요소만 따로 떼어 ‘ & ‘ 붙이기
1 | function list(names) { |
정규식
1 | var list = (names) => names.map(x => x.name).join(', ').replace(/(.*),(.*)$/, "$1 &$2") |
# thoughts
- 여전히 정규식은 어렵게 다가온다.